/*
* Conditions Of Use
*
* This software was developed by employees of the Sigmatrix(Beijing) Corporation.
* This software is provided by sigmatrix as a service and is expressly
* provided "AS IS."  Sigmatrix MAKES NO WARRANTY OF ANY KIND, EXPRESS, IMPLIED
* OR STATUTORY, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTY OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, NON-INFRINGEMENT
* AND DATA ACCURACY.  Sigmatrix does not warrant or make any representations
* regarding the use of the software or the results thereof, including but
* not limited to the correctness, accuracy, reliability or usefulness of
* the software.
*
* Permission to use this software is contingent upon your acceptance
* of the terms of this agreement.
*
*/
package com.sigmatrix.h5.service.impl;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.sigmatrix.h5.dao.MultistageLevelQrCodeDao;
import com.sigmatrix.h5.dto.PackageScanData;
import com.sigmatrix.h5.dto.ProductData;
import com.sigmatrix.h5.entity.MultistageLevelQRCode;
import com.sigmatrix.h5.enums.ResponseEnum;
import com.sigmatrix.h5.exception.DaoException;
import com.sigmatrix.h5.response.Response;
import com.sigmatrix.h5.service.PackageScanService;
import com.sigmatrix.h5.service.common.impl.BaseServiceImpl;
import com.sigmatrix.h5.utils.Cons;
import com.sigmatrix.h5.web.vo.PackageScanVo;

/**
 *@ClassName: PackageScanServiceImpl
 *@Description: TODO(包装管理码扫描业务层实现类)
 *@author liuzelei
 *@date 2017年7月8日 下午3:47:42
 */
@Service
public class PackageScanServiceImpl extends BaseServiceImpl<PackageScanData> implements PackageScanService{
	
	/**logger record*/
	private static final Logger LOGGER = LoggerFactory.getLogger(PackageScanServiceImpl.class);
	
	@Autowired
	private transient MultistageLevelQrCodeDao multistageLevelQRCodeDao;
	
	/**(non-Javadoc)
	 *@Title: scan
	 *@Description: 
	 *@see com.sigmatrix.h5.service.PackageScanService#scan(com.sigmatrix.h5.web.vo.PackageScanVo)
	 *@author liuzelei
	 *@date 2017年7月8日 下午3:50:03
	 */
	@Override
	public Response scan(PackageScanVo vo) {
		if(Cons.PACKAGE_SCAN_LEVEL_1 == vo.getLevel()){// 1:拆补箱下的瓶码数
			return dealLevel1(vo);
		}else if(Cons.PACKAGE_SCAN_LEVEL_2 == vo.getLevel()){// 2:拆补垛下的箱码数及垛
			dealLevel2(vo);
		}else if(Cons.PACKAGE_SCAN_LEVEL_2 == vo.getLevel()){// 3:补垛查询虚拟垛码 
			dealLevel3(vo);
		}
		
		return null;
	}

	/**
	 *@Title: dealLevel3
	 *@Description: TODO()
	 *@return void
	 *@author liuzelei
	 *@date 2017年7月8日 下午3:50:38
	 *@param vo
	 */
	private void dealLevel3(PackageScanVo vo) {
		
	}

	/**
	 *@Title: dealLevel2
	 *@Description: TODO()
	 *@return void
	 *@author liuzelei
	 *@date 2017年7月8日 下午3:50:37
	 *@param vo
	 */
	private void dealLevel2(PackageScanVo vo) {
		
	}

	/**
	 *@Title: dealLevel1
	 *@Description: TODO(拆补箱下的瓶码数)
	 *@return void
	 *@author liuzelei
	 *@date 2017年7月8日 下午3:50:34
	 *@param vo
	 */
	private Response dealLevel1(PackageScanVo vo) {
		try {
			final MultistageLevelQRCode mlqrc = multistageLevelQRCodeDao.findMultistageLevelQRCodeByQRCode(vo.getEnterpriseCode(),vo.getCode());
			
			if(null == mlqrc || null == mlqrc.getProductId()){
				return new Response(Cons.RESPONSE_CODE_DATA_EXCEPTION,"箱码信息不完整或不存在");
			}
			
			ProductData product = null;
			try {
				//TODO
//				product = productService.getProductFromCache(mlqrc.getProductId(), vo.getEnterpriseCode());
				PackageScanData psd = new PackageScanData();
				psd.setCount(mlqrc.getChildNum());
				psd.setProductId(mlqrc.getProductId());
				psd.setProductName(null == product ? "" : product.getProductName());
				return new Response(ResponseEnum.SUCCESS,psd);
			} catch (Exception e) {
				LOGGER.error("查询异常>PackageScan<",e);
				return new Response(Cons.RESPONSE_CODE_EXCEPTION,"查询异常");
			}
			
		} catch (DaoException e) {
			LOGGER.error("查询异常>PackageScan<",e);
			return new Response(Cons.RESPONSE_CODE_EXCEPTION,"查询异常");
		}
	}
	
}
